package com.qs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qs.exception.ServiceException;
import com.qs.mapper.GoodsMapper;
import com.qs.mapper.StoreCommentMapper;
import com.qs.mapper.UserMapper;
import com.qs.model.dto.CommentDto;
import com.qs.model.dto.StoreDto;
import com.qs.model.dto.StoreGoodsDto;
import com.qs.model.entity.*;
import com.qs.mapper.StoreMapper;
import com.qs.response.ResponseEnum;
import com.qs.service.IStoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 店铺表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-03-25
 */
@Service
@RequiredArgsConstructor
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {

    private final GoodsMapper goodsMapper;
    private final StoreCommentMapper storeCommentMapper;
    private final UserMapper userMapper;

    @Override
    public StoreDto queryStoreDetail(Integer storeId) {
        QueryWrapper<Store> wrapper = new QueryWrapper<>();
        wrapper.eq("id", storeId);
        wrapper.eq("status","通过");
        Store store = baseMapper.selectOne(wrapper);
        StoreDto storeDto = new StoreDto();
        BeanUtils.copyProperties(store,storeDto);
        return storeDto;
    }

    @Override
    public List<StoreGoodsDto> queryGoodsList(Integer storeId) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id", storeId);
        wrapper.eq("status","上架");
        List<Goods> goodsList = goodsMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(goodsList)){
            List<StoreGoodsDto> list = new ArrayList<>();
            goodsList.stream().forEach(goods -> {
                StoreGoodsDto storeGoodsDto = new StoreGoodsDto();
                BeanUtils.copyProperties(goods, storeGoodsDto);
                list.add(storeGoodsDto);
            });
            return list;
        }
        return null;
    }

    @Override
    public List<CommentDto> queryCommentList(Integer storeId) {
        if(null == storeId){
            throw new ServiceException(ResponseEnum.BAD_REQUEST);
        }
        QueryWrapper<StoreComment> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id", storeId);
        wrapper.orderByDesc("create_time");
        List<StoreComment> comments = storeCommentMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(comments)){
            List<CommentDto> list = new ArrayList<>();
            comments.stream().forEach(comment ->{
                User user = userMapper.selectById(comment.getUserId());

                CommentDto commentDto = new CommentDto();
                commentDto.setAvatar(user.getAvatar());
                commentDto.setId(comment.getId());
                commentDto.setComment(comment.getComment());
                commentDto.setScore(comment.getScore());
                commentDto.setUsername(user.getNickName());
                commentDto.setCreateTime(comment.getCreateTime());

                list.add(commentDto);
            });
            return list;
        }
        return null;
    }
}
